<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017-07-28
 * Time: 17:51
 */

namespace Qwadmin\Controller;


class ActivityController extends ComController
{
    /**
     * 显示活动页面
     */
    public function index(){
        $list = D('activity')->where('type = 1')->select();
        $this->assign('list',$list);
        $this->display();
    }

    //活动详情页
    public function indexall(){
        $list = D('activity')->select();
        $this->assign('list',$list);
        $this->display();
    }

    /**
     * 通过ajax查询小说
     */
    public function findnovel(){



        $novel_name = $_POST['novel_name'];
        //dump($novel_name);die;
        //$novel_id = D('novel')->query("select id from qw_novel where name='$novel_name'");
        $novel = D('novel')->where("name='$novel_name'")->find();

        if (empty($novel['id'])) {
            $data['msg'] = '没有此小说,请您核对小说名';
            $data['status'] = 201;

        }else{
            $data['msg'] = '有这部小说';
            $data['novel_id'] = $novel['id'];
            $data['status'] = 200;
        }
        echo json_encode($data);
    }
    /**
     * 通过ajax修改小说活动id
     */
    public function ajaxnovel(){
        $novel_name = $_POST['novel_name'];
       echo  D("novel")->where("name = '$novel_name'")->save(['activity_id'=>0]);

    }

    /**
     * @param int $sid
     * @param int 新增活动
     */
    public function addactivity()
    {
        if(empty($_POST)){
            $this->display('formactivity');
        }else{
            //接受数据


            isset($_POST['status'])?$_POST['status']:0;
            isset($_POST['type'])?$_POST['status']:1;
            isset($_POST['name'])?$_POST['name']:'';
            isset($_POST['description'])?$_POST['description']:'';
            isset($_POST['start_time'])?$_POST['start_time']:'';
            isset($_POST['end_time'])?$_POST['end_time']:'';
            //赋值一个变量添加到数据库中
            $data['status'] = $_POST['status'];
            $data['name'] = $_POST['name'];
            $data['description'] = $_POST['description'];
            $data['start_time'] = $_POST['start_time'];
            $data['end_time'] = $_POST['end_time'];

            //插入数据库
            $arctivity_id = M('activity')->data($data)->add();

            //遍历传过来数组(小说名字)
            foreach ($_POST['novel_name'] as $novel_name){
                //查询小说id
                $novel = D('novel')->where("name='$novel_name'")->find();
                if (!empty($novel)) {
                    $novel_id = $novel['id'];
                    //根据小说id将相应的小说的活动字段修改了
                    D("novel")->where("id = $novel_id")->save(['activity_id'=>$arctivity_id]);
                }
            }
			addlog('添加活动');
            $this->success('恭喜！新增活动成功！', "index.html");
        }
    }

    /**
     * 回显活动
     */
    public function edit($id)
    {
        $id = intval($id);
        //查找小说
        $novel_name = M('novel')->where('activity_id='.$id)->select();

        $activity = M('activity')->where('id=' . $id)->find();


        if ($activity) {
            $this->assign('activity', $activity);
            $this->assign('novel_name',$novel_name);
        } else {
            $this->error('参数错误！');
        }
        $this->display('editactivity');

    }

    /**
     * 修改活动
     */
    public function updateactivity(){
        $id = $_POST['id'];
        isset($_POST['status'])?$_POST['status']:0;
        isset($_POST['name'])?$_POST['name']:'';
        isset($_POST['description'])?$_POST['description']:'';
        isset($_POST['start_time'])?$_POST['start_time']:'';
        isset($_POST['end_time'])?$_POST['end_time']:'';
        //赋值一个变量添加到数据库中
        $data['status'] = $_POST['status'];
        $data['name'] = $_POST['name'];
        $data['description'] = $_POST['description'];
        $data['start_time'] = $_POST['start_time'];
        $data['end_time'] = $_POST['end_time'];

        //插入数据库
        M('activity')->data($data)->where("id='$id'")->save();
        //dump($arctivity_id);die;



        //遍历传过来数组(小说名字)
        foreach ($_POST['novel_name'] as $novel_name){
            //查询小说id
            $novel = D('novel')->where("name='$novel_name'")->find();
            if (!empty($novel)) {
                $novel_id = $novel['id'];
                //根据小说id将相应的小说的活动字段修改了
                D("novel")->where("id = $novel_id")->save(['activity_id'=>$id]);
            }
        }
		addlog('修改活动，activity_id：' . $id);
        $this->success('恭喜！修改活动成功！', "index.html");
    }

    /**
     * 删除活动
     */
    public function del()
    {

        $id = isset($_REQUEST['id']) ? $_REQUEST['id'] : false;
        if ($id) {
            if (is_array($id)) {
                $id = implode(',', $id);
                $map['id'] = array('in', $id);
            } else {
                $map = 'id=' . $id;
            }
            if (M('activity')->where($map)->delete()) {
				//修改小说活动id为0
				$mapp['activity_id'] = $map['id'];
				M('novel')->where($mapp)->save(['activity_id'=>0]);
				addlog('删除活动，activity_id：' . $id);
                $this->success('恭喜，活动删除成功！');
            } else {
                $this->error('参数错误！');
            }
        } else {
            $this->error('参数错误！');
        }

    }
	
	
	
	/**
	 *返书币 
	 */
	public function indexback(){
        $list = D('activity')->where("type = 2")->select();
        $this->assign('list',$list);
        $this->display();
    }



    public function addactivityback()
    {
        if(empty($_POST)){
            $this->display('addactivityback');
        }else{
            //接受数据
            isset($_POST['status'])?$_POST['status']:0;
            isset($_POST['type'])?$_POST['status']:2;
            isset($_POST['name'])?$_POST['name']:'';
            isset($_POST['description'])?$_POST['description']:'';
            isset($_POST['start_time'])?$_POST['start_time']:'';
            isset($_POST['end_time'])?$_POST['end_time']:'';
            isset($_POST['url'])?$_POST['url']:'';
            $subscribe_money = isset($_POST['subscribe'])?intval($_POST['subscribe']):0;
            $recharge_money = isset($_POST['recharge'])?$_POST['recharge']:'0%';
            //赋值一个变量添加到数据库中
            $data['status'] = $_POST['status'];
            $data['name'] = $_POST['name'];
            $data['description'] = $_POST['description'];
            $data['start_time'] = $_POST['start_time'];
            $data['end_time'] = $_POST['end_time'];
            $data['subscribe_money'] = $subscribe_money;
            $data['recharge_money'] = intval($recharge_money)/100;
            $data['type']  = 2;
            $data['url'] =$_POST['url'];
            //插入数据库
            $arctivity_id = M('activity')->data($data)->add();
            addlog('添加活动');
            $this->success('恭喜！新增活动成功！', "indexback.html");
        }
    }



    /**
     * 修改活动返书币
     */
    public function updateactivityback(){
        $id = $_POST['id'];
        isset($_POST['status'])?$_POST['status']:0;
        isset($_POST['type'])?$_POST['status']:2;
        isset($_POST['name'])?$_POST['name']:'';
        isset($_POST['description'])?$_POST['description']:'';
        isset($_POST['start_time'])?$_POST['start_time']:'';
        isset($_POST['end_time'])?$_POST['end_time']:'';
        isset($_POST['url'])?$_POST['url']:'';
        $subscribe_money = isset($_POST['subscribe'])?intval($_POST['subscribe']):0;
        $recharge_money = isset($_POST['recharge'])?$_POST['recharge']:'0%';
        //赋值一个变量添加到数据库中
        $data['status'] = $_POST['status'];
        $data['name'] = $_POST['name'];
        $data['description'] = $_POST['description'];
        $data['start_time'] = $_POST['start_time'];
        $data['end_time'] = $_POST['end_time'];
        $data['subscribe_money'] = $subscribe_money;
        $data['recharge_money'] = intval($recharge_money)/100;
        $data['type']  = 2;
        $data['url'] =$_POST['url'];
        M('activity')->data($data)->where("id='$id'")->save();
        addlog('修改活动，activity_id：' . $id);
        $this->success('恭喜！修改活动成功！', "indexback.html");
    }


    /**
     * 回显活动
     */
    public function editback($id)
    {
        $id = intval($id);

        $activity = M('activity')->where('id=' . $id)->find();


        if ($activity) {
            $this->assign('activity', $activity);
        } else {
            $this->error('参数错误！');
        }
        $this->display('editactivityback');

    }


    /**
     * 分享送书币
     */
    public function share(){
    	$list = D('activity')->where("type = 3")->select();
        $this->assign('list',$list);
        $this->display();
    }

    /**
     * 添加分享送书币活动
     */
    public function shareform(){
        if(empty($_POST)){
            $this->display();
        }else{
            //接受数据
            //dump($_POST);die;
            isset($_POST['status'])?$_POST['status']:0;
            isset($_POST['type'])?$_POST['status']:3;
            isset($_POST['name'])?$_POST['name']:'';
            isset($_POST['description'])?$_POST['description']:'';
            isset($_POST['start_time'])?$_POST['start_time']:'';
            isset($_POST['end_time'])?$_POST['end_time']:'';
            isset($_POST['url'])?$_POST['url']:'';
            $subscribe_money = isset($_POST['subscribe'])?intval($_POST['subscribe']):0;
            $recharge_money = isset($_POST['recharge'])?$_POST['recharge']:'0%';
            //赋值一个变量添加到数据库中
            $data['status'] = $_POST['status'];
            $data['name'] = $_POST['name'];
            $data['description'] = $_POST['description'];
            $data['start_time'] = $_POST['start_time'];
            $data['end_time'] = $_POST['end_time'];
            $data['subscribe_money'] = $subscribe_money;
            $data['recharge_money'] = intval($recharge_money)/100;
            $data['type']  = $_POST['type'];
            $data['url'] =$_POST['url'];
            //dump($data);die;
            //插入数据库
            $arctivity_id = M('activity')->data($data)->add();
            addlog('添加活动');
            $this->success('恭喜！新增活动成功！', "share.html");
        }
    }


    public function shareedit($id){
        $activity = D('activity')->where("id='$id'")->find();
        $this->assign('activity',$activity);
        $this->display();
    }

    public function shareupdate(){
        $id = $_POST['id'];
        //dump($_POST);die;
        isset($_POST['status'])?$_POST['status']:0;
        isset($_POST['type'])?$_POST['status']:3;
       
        isset($_POST['description'])?$_POST['description']:'';
        isset($_POST['start_time'])?$_POST['start_time']:'';
        isset($_POST['end_time'])?$_POST['end_time']:'';
        
        $subscribe_money = isset($_POST['subscribe'])?intval($_POST['subscribe']):0;
        
        //赋值一个变量添加到数据库中
        $data['status'] = $_POST['status'];
       
        $data['description'] = $_POST['description'];
        $data['start_time'] = $_POST['start_time'];
        $data['end_time'] = $_POST['end_time'];
        $data['subscribe_money'] = $subscribe_money;
       
        $data['type']  = $_POST['type'];
        
        M('activity')->data($data)->where("id='$id'")->save();
        addlog('修改活动，activity_id：' . $id);
        $this->success('恭喜！修改活动成功！', "share.html");
    }
	
	
	
	
	
	
	//充值活动赠送显示
	public function payindex(){
		//活动
		$list = D('activity')->where("type = 4")->select();
        $this->assign('list',$list);
		//充值表
		$paylist = D('payactive')->where('activity_id = 55')->order('bamount asc')->select();
		 $this->assign('paylist',$paylist);
        $this->display();
    }
	
	
	
	  // /**
     // * //充值活动赠送显示回显
     // */
     public function activityedit($id)
     {
         $id = intval($id);

         $activity = M('activity')->where('id=' . $id)->find();
         if ($activity) {
             $this->assign('activity', $activity);
         } else {
             $this->error('参数错误！');
         }
         $this->display('payeditactivity');

     }


     /**
      * //充值活动赠送修改时间 状态
      */
     public function payupdateactivity(){
         $id = $_POST['id'];
         isset($_POST['status'])?$_POST['status']:0;
         isset($_POST['type'])?$_POST['type']:4;
         isset($_POST['name'])?$_POST['name']:'';
         isset($_POST['description'])?$_POST['description']:'';
         isset($_POST['start_time'])?$_POST['start_time']:'';
         isset($_POST['end_time'])?$_POST['end_time']:'';
         $samount = isset($_POST['samount'])?intval($_POST['samount']):0;
		 $bamount = isset($_POST['bamount'])?intval($_POST['bamount']):0;
		 $returnmoney = isset($_POST['returnmoney'])?intval($_POST['returnmoney']):0;
         //赋值一个变量添加到数据库中
         $data['status'] = $_POST['status'];
         $data['name'] = $_POST['name'];
         $data['description'] = $_POST['description'];
         $data['start_time'] = $_POST['start_time'];
         $data['end_time'] = $_POST['end_time'];
         $data['type']  = 4;
         M('activity')->data($data)->where("id='$id'")->save();
         addlog('修改活动，activity_id：' . $id);
         $this->success('恭喜！修改活动成功！', "payindex.html");
     }


 
	 
	 
	 //支付payactive增加
     public function payadd()
     {
         if(empty($_POST)){
             $this->display('payadd');
         }else{
             //接受数据
             $samount = isset($_POST['samount'])?intval($_POST['samount']):0;
             $bamount = isset($_POST['bamount'])?intval($_POST['bamount']):0;
             $amount = isset($_POST['amount'])?intval($_POST['amount']):0;
             $pay_id = isset($_POST['pay_id'])?intval($_POST['pay_id']):0;
             $returnmoney = isset($_POST['returnmoney'])?$_POST['returnmoney']:0;
			 //图片
			 $str = C('FILE_HOST_PATH');
			  $img = str_replace($str,"",I('post.thumbnail', '', 'strip_tags'));
			   $map['img'] = $img;
			   $map['samount'] = $samount;
			   $map['bamount'] = $bamount;
			   $map['amount'] = $amount;
			   $map['pay_id'] = $pay_id;
			   $map['activity_id'] = 55;
			   $map['returnmoney'] = $returnmoney;
			   M('payactive')->data($map)->add();
              addlog('添加范围');
              $this->success('恭喜！新增范围成功！', "payindex.html");
         }
     }
	 
	 
	 
	 	
      //充值活动赠送显示回显
     // */
     public function paedit($id)
     {
         $id = intval($id);

         $activity = M('payactive')->where('pid=' . $id)->find();
         if ($activity) {
             $this->assign('activity', $activity);
         } else {
             $this->error('参数错误！');
         }
         $this->display('paedit');

     }
	 
	 
	 
	 	 //支付payactive增加
     public function payupdate()
     {
			$pid = $_POST['id'];
			if(empty($pid)){
				  $this->error('参数错误！');
			}
             //接受数据
             $samount = isset($_POST['samount'])?intval($_POST['samount']):0;
             $bamount = isset($_POST['bamount'])?intval($_POST['bamount']):0;
             $amount = isset($_POST['amount'])?intval($_POST['amount']):0;
             $pay_id = isset($_POST['pay_id'])?intval($_POST['pay_id']):0;
             $returnmoney = isset($_POST['returnmoney'])?$_POST['returnmoney']:0;
			 //图片
			 $str = C('FILE_HOST_PATH');
			  $img = str_replace($str,"",I('post.thumbnail', '', 'strip_tags'));
			   $map['img'] = $img;
			   $map['samount'] = $samount;
			   $map['bamount'] = $bamount;
			   $map['amount'] = $amount;
			   $map['pay_id'] = $pay_id;	   
			   $map['returnmoney'] = $returnmoney;
			   M('payactive')->data($map)->where('pid = '.$pid)->save();
              addlog('修改范围');
              $this->success('恭喜！修改范围成功！', "payindex.html");
			  
     }
	 
	 
	  
	 public function delete(){
		 
        $id = isset($_REQUEST['id']) ? $_REQUEST['id'] : false;
        if ($id) {
            if (is_array($id)) {
                $id = implode(',', $id);
                $map['pid'] = array('in', $id);
            } else {
                $map = 'pid=' . $id;
            }
            if (M('payactive')->where($map)->delete()) {
				
                $this->success('恭喜，范围删除成功！');
            } else {
                $this->error('参数错误！');
            }
        } else {
            $this->error('参数错误！');
        }
	 }

	
	
	
	

    
}